"!locate filetype.vim
"vimrc for c,cpp,javascript

"set filetype by filename extension
au! BufNewFile,BufRead *.gtkrc setf gtkrc
au! BufNewFile,BufRead *.qml setf qml

autocmd FileType c,cpp,javascript,gtkrc,qml,qss set sw=4 shiftwidth=4 expandtab ts=4 fdm=syntax

"vimrc for python
autocmd FileType python set fdm=indent

"vimrc for sh
autocmd FileType sh set shiftwidth=4  ts=4 expandtab fdm=indent

"vimrc for text
autocmd FileType text set  et sw=4 ts=4 sts=4


if &diff
    "syntax off
    colorscheme evening
endif

"https://www.cnblogs.com/andy071001/articles/3048901.html
"设置taglist, 需要下载taglist插件
"TlistUpdate可以更新tags
map <F3> :silent! Tlist<CR> "按下F3就可以呼出了
let Tlist_Ctags_Cmd='ctags' "因为我们放在环境变量里，所以可以直接执行
let Tlist_Use_Right_Window=1 "让窗口显示在右边，0的话就是显示在左边
let Tlist_Show_One_File=0 "让taglist可以同时展示多个文件的函数列表，如果想只有1个，设置为1
let Tlist_File_Fold_Auto_Close=1 "非当前文件，函数列表折叠隐藏
let Tlist_Exit_OnlyWindow=1 "当taglist是最后一个分割窗口时，自动推出vim
let Tlist_Process_File_Always=0 "是否一直处理tags.1:处理;0:不处理。不是一直实时更新tags，因为没有必要
let Tlist_Inc_Winwidth=0


"cscope 
if has("cscope")
    "设置按F12就更新tags的方法  
    map <F12> :call Do_CsTag()<CR>  
    "使支持用 Ctrl+]  和 Ctrl+t 快捷键在代码间跳来跳去
    set cscopetag
    "check cscope for definition of a symbol before checking ctags:
    "set to 1 if you want the reverse search order.
    set csto=1

    "add any cscope database in current directory
    if filereadable("cscope.out")
        cs add cscope.out
    "else add the database pointed to by environment variable
    elseif $CSCOPE_DB !=""
        cs add $CSCOPE_DB
    endif

    "show msg when any other cscope db added
    set cscopeverbose
    "in milliseconds
    "set timeoutlen=4000 
    "first click ctrl+\ in the same time, then click s
    nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR> 
    nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <C-\>t :cs find t <C-R>=expand("<cWORD>")<CR><CR>:copen<CR>
    nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
    nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>
    nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>
    "使用quickfix显示cscope查询结果
    set cscopequickfix=c-,d-,e-,g-,i-,s-,t-
endif

"cs find s: 查找C语言符号，即查找函数名、宏、枚举值等出现的地方
"cs find g: 查找函数、宏、枚举等定义的位置，类似ctags所提供的功能
"cs find d: 查找本函数调用的函数。
"cs find c: 查找调用本函数的函数
"cs find t: 查找指定的字符串
"cs find e: 查找egrep模式，相当于egrep功能，但查找速度快多了
"cs find f: 查找并打开文件，类似vim的find功能
"cs find i: 查找包含本文件的文件

"https://www.cnblogs.com/opennaive/p/3312794.html
"https://blog.csdn.net/quantumpo/article/details/8779393
function Do_CsTag()
    let dir = getcwd()
    if filereadable("tags")
        let tagsdeleted=delete("./"."tags")
        if(tagsdeleted!=0)
            echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None
            return
        endif
    endif
    if has("cscope")
        silent! execute "cs kill -1"
    endif
    if filereadable("cscope.files")
        let csfilesdeleted=delete("./"."cscope.files")
        if(csfilesdeleted!=0)
            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None
            return
        endif
    endif
    if filereadable("cscope.out")
        let csoutdeleted=delete("./"."cscope.out")
        if(csoutdeleted!=0)
            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None
            return
        endif
    endif
    if(executable('ctags'))
        "silent! execute "!ctags -R --c-types=+p --fields=+S *"
        silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
    endif
    if(executable('cscope') && has("cscope") )
        "silent! execute "!find `pwd .` -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' > cscope.files"
        silent! execute "!find `pwd .` \\! -path '*/.pc/*' \\! -path '*/.git/*' \\! -path '*/debian/*' \\( -name '*.h' -o -name '*.hpp' -o -name '*.c' -o -name '*.cc' -o -name '*.cpp' \\) -type f > cscope.files"
        silent! execute "!cscope -kb"
        execute "normal :"
        if filereadable("cscope.out")
            execute "cs add cscope.out"
        endif
    endif
    execute "redraw!"
endfunction

" Set cursor shape and color
if &term =~ "xterm"
    " INSERT mode
    let &t_SI = "\<Esc>[1 q" . "\<Esc>]12;red\x7"
    " REPLACE mode
    let &t_SR = "\<Esc>[1 q" . "\<Esc>]12;red\x7"
    " NORMAL mode
    let &t_EI = "\<Esc>[1 q" . "\<Esc>]12;red\x7"
    set cursorline
endif
" 1 -> blinking block  闪烁的方块
" 2 -> solid block  不闪烁的方块
" 3 -> blinking underscore  闪烁的下划线
" 4 -> solid underscore  不闪烁的下划线
" 5 -> blinking vertical bar  闪烁的竖线
" 6 -> solid vertical bar  不闪烁的竖线" Set cursor shape and color
